package com;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.KeyEvent;

public class Plane {
    Image image;
    int x = 300;
    int y = 800;
    boolean up = false;
    boolean down = false;
    boolean left = false;
    boolean right = false;
    int step = 10;
    public Plane(){
        image = ImageIOUtils.getImage(this,"images/myplane.png");
    }

    public void draw(Graphics graphics) {
        graphics.drawImage(image,this.x,this.y,null);
        if(up==true&down==false&left==false&right==false){
            y = y - step;
        }else if(up==false&down==true&left==false&right==false){
            y = y + step;
        }else if(!up&!down&left&!right){
            x = x - step;
        }else if(!up&!down&!left&right){
            x = x + step;
        }else if(up&!down&left&!right){//上左
            y = y - step;
            x = x - step;
        }else if(!up&down&left&!right){//下左
            y = y + step;
            x = x - step;
        }else if(up&!down&!left&right){//上右
            y = y - step;
            x = x + step;
        }else if(!up&down&!left&right){//下右
            y = y + step;
            x = x + step;
        }
        checkXY();
    }

    public void checkXY(){
        //判断x，y坐标是否越界，如果要越界不准
        if(x<=0){
            x = 0;
        }
        if(x>=600){
            x= 600;
        }
        if(y<=0){
            y=0;
        }
        if(y>=850){
            y=850;
        }
    }
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_UP){
            up = true;
        }else if(e.getKeyCode() == KeyEvent.VK_DOWN){
            down = true;
        }else if(e.getKeyCode() == KeyEvent.VK_LEFT){
            left = true;
        }else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
            right = true;
        }
    }

    public void keyReleased(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_UP){
            up = false;
        }else if(e.getKeyCode() == KeyEvent.VK_DOWN){
            down = false;
        }else if(e.getKeyCode() == KeyEvent.VK_LEFT){
            left = false;
        }else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
            right = false;
        }
    }

    public Rectangle getRectangle(){
        return new Rectangle(x,y,image.getWidth(null),image.getHeight(null));
    }
}
